<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<script>
    // var 函数作用域   let 块作用域
    for (var i = 0; i < 5; i++) { // 5 5 5 5 5
        setTimeout(() => {
            console.log(i) // 父子作用域的不同，在执行setTimeOut时，i变量已经是5了
        }, 1000);
    }

    for (let i = 0; i < 5; i++) { // 0 1 2 3 4
        setTimeout(() => {
            console.log(i)
        }, 1000);
    }


    // for in 循环  对对象的操作
    var obj = {
        'zs': 21,
        'ls': 25,
        'zw': 23
    }
    for (let key in obj) {
        console.log(key + ':' + obj[key])
    }

    // for each 循环 对数组进行操作 ，三个参数：当前值，索引，数组
    var myArr = [10, 20, 25, 30, 42, 52]
    var sum = 0
    myArr.forEach((value, index, arr) => {
        sum += value
    })
    console.log(sum);

    // for of 对数组，字符串来遍历
    var test = '123abc'
    for (var index of test) {
        console.log(index);
    }
</script>
</body>

</html>
